package com.nit.lab.service.medical.impl;

import com.alibaba.fastjson.JSON;
import com.nit.lab.dto.medical.InfoDTO;
import com.nit.lab.dto.utils.PagingDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.entity.medical.InfoEntity;
import com.nit.lab.mapper.medical.InfoMapper;
import com.nit.lab.service.medical.InfoService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class InfoServiceImpl implements InfoService {

    private final InfoMapper infoMapper;

    @Autowired(required = false)
    public InfoServiceImpl(InfoMapper infoMapper) {
        this.infoMapper = infoMapper;
    }

    @Override
    public TableRspDTO list4Table(TableReqDTO tableReqDTO) throws Exception {
        Long count = infoMapper.count4Table(
                tableReqDTO.parseQueryParam("sensorId"));
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                infoMapper.list4Table(
                        tableReqDTO.getStart(),
                        tableReqDTO.getPageSize(),
                        tableReqDTO.parseQueryParam("sensorId"),
                        tableReqDTO.getSortField(),
                        tableReqDTO.getSortOrder()));
        TableRspDTO tableRspDTO = new TableRspDTO(pagingDTO);
        return tableRspDTO;
    }

    @Override
    public Integer add(InfoDTO infoDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        InfoEntity infoEntity = mapperFactory.getMapperFacade().map(infoDTO, InfoEntity.class);
        infoEntity.setGmtCreate(new Date());
        return infoMapper.add(infoEntity);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(InfoDTO infoDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        InfoEntity infoEntity = mapperFactory.getMapperFacade().map(infoDTO, InfoEntity.class);
        return infoMapper.update(infoEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String infoIds) throws Exception {
        String[] arrayInfoId = infoIds.split(",");
        for (int i = 0; i < arrayInfoId.length; i++) {
            infoMapper.remove(Long.parseLong(arrayInfoId[i]));
        }
        return arrayInfoId.length;
    }

    @Override
    public List<InfoDTO> getInfo(InfoDTO infoDTO) throws Exception {
        List<InfoDTO> res = infoMapper.getInfo(infoDTO.getSensorId());
        List<InfoDTO> counts = infoMapper.countInfo(infoDTO.getSensorId());
        return res.subList(res.size() - counts.get(counts.size() - 1).getCount(), res.size());
    }
}
